package password

import (
	"bytes"
	"shop-common/utils/crypto"
	"shop-common/utils/rand"
	"time"
)

func GenerateSalt() string {
	ranStr := time.Now().String() + rand.KrandAll(32)
	var cipherTxt bytes.Buffer
	cipherTxt.WriteString(ranStr)

	return rand.RandString(88, crypto.SHA1(cipherTxt.Bytes()))
}

func GeneratePassword(password string, salt string) string {
	var str = "name:" + "bob" + "pwd," + password + ":salt:" + salt + ":uniMall"
	var cipherTxt bytes.Buffer
	cipherTxt.WriteString(str)
	return crypto.SHA1(cipherTxt.Bytes())
}

func Check(dbPassword string, dbSalt string, password string) bool {
	return dbPassword == GeneratePassword(password, dbSalt)
}
